﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net.Sockets;
using System.Text;

namespace OnCommon
{
    public class SocketTcpDataBase
    {
        internal Socket AccpetSocket { get; set; }
        internal PacketType SendDataType { get; set; }
        public PacketType DataType { get; set; }

        public System.Net.IPEndPoint IPEndPoint { get; set; }

        SocketTcpClient _client = new SocketTcpClient();
        public void Send(object obj)
        {
            _client._Socket = this.AccpetSocket;
            _client.Send(obj);
        }
        public void Send(FileStream fs)
        {
            _client._Socket = this.AccpetSocket;
            _client.Send(fs, buffLen: 10240);
        }
    }

    public class SocketTcpData : SocketTcpDataBase
    {
        public string GetStringData()
        {
            //if (DataType == PacketType.String)
            {
                return Encoding.UTF8.GetString(this.ByteData);
            }
            //return null;
        }

        public T GetObjData<T>()
        {
            if (DataType == PacketType.Complex)
            {
                var msg = Encoding.UTF8.GetString(this.ByteData);
                return Newtonsoft.Json.JsonConvert.DeserializeObject<T>(msg);
            }
            return default(T);
        }

        public byte[] GetByteData()
        {
            return this.ByteData;
        }

        public byte[] ByteData { get; set; }
    }

    public class SocketTcpFileData : SocketTcpDataBase
    {
        public string FileName { get; set; }

        public byte[] CurrentBytes { get; internal set; }

        public int CurrentFileLen { get; internal set; }

        internal bool _HasFirst = true;
        public bool HasFirst
        {
            get
            {
                return _HasFirst;
            }
        }
    }
}
